package com.ff.xyh.access.controller.access;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.access.service.access.AccessRoleService;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.useraccess.AccessRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther FF
 * @Date 2023年 01月 15日
 * @Time 08:01
 */


@Api(tags = "后台角色接口")
@RestController
@RequestMapping("/access/role")
public class AccessRoleController {

    @Autowired
    private AccessRoleService accessRoleService;

    @ApiOperation(value = "查询所有角色")
    @GetMapping("get/list/all")
    public ResultObject getAllAccessRole() {
        List<AccessRole> list = accessRoleService.list(null);
        if (list == null || list.isEmpty()){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok().data("items", list);
        }
    }

//    @ApiOperation(value = "根据id查询角色")
//    @GetMapping("get/{id}")
//    public ResultObject getAccessRoleById(
//            @PathVariable Integer id) {
//        AccessRole accessRole = accessRoleService.getById(id);
//        if (accessRole == null ){
//            return ResultObject.error().message(ResultMessage.NO_RESULT);
//        } else {
//            return ResultObject.ok().data("data", accessRole);
//        }
//    }


    @ApiOperation(value = "根据 roleId 删除角色")
    @PostMapping("delete/{roleId}")
    public ResultObject deleteAccessRole(
            @PathVariable String roleId) {
        AccessRole accessRole = accessRoleService.getById(roleId);
        if (accessRole == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (accessRoleService.removeById(roleId)) {
                return ResultObject.ok().data("data", accessRole);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        }
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("add")
    public ResultObject addAccessRole (
            @RequestBody AccessRole accessRole) {
        QueryWrapper< AccessRole > wrapper = new QueryWrapper<>();
        wrapper.eq("role", accessRole.getRole());
        if (accessRoleService.getOne(wrapper) != null){
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_EXISTS);
        } else {
            if (accessRoleService.save(accessRole)) {
                return ResultObject.ok().data("data", accessRole);
            } else {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT_BUT_FAILED_TO_INSERT);
            }
        }
    }

    @ApiOperation(value = "修改角色")
    @PostMapping("modify")
    public ResultObject modifyAccessRole (
            @RequestBody AccessRole accessRole) {
        if (accessRoleService.getById(accessRole.getId()) == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (accessRoleService.updateById(accessRole)) {
                return ResultObject.ok().putData(accessRole);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_UPDATE);
            }
        }
    }

//    /**
//     * 获取管理员ID获取角色列表
//     * @param userId
//     * @return
//     */
//    @ApiOperation(value = "根据 userId 获取用户的所有角色")
//    @GetMapping("get/user/{userId}")
//    public ResultObject getRolesByUserId (
//            @RequestParam(name = "userId") String userId ) {
//        List<AccessRole> accessRoleList = accessRoleService.getRolesByUserId(userId);
//        if (accessRoleList == null || accessRoleList.isEmpty())
//            return ResultObject.error().message(ResultMessage.NO_RESULT);
//        else
//            return ResultObject.ok().data("items", accessRoleList);
//    }

}
